package com.openbravo.pos.panels.navigator;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.I_ParentNode;
import cn.ekuma.data.dao.bean.IKeyed;
import cn.ekuma.data.dao.bean.I_Category;
import cn.ekuma.data.dao.user.BrowsableEditableDataModel;
import cn.ekuma.epos.db.table.I_Customer;

import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.CustomerCategory;
import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.JListNavigator;
import com.openbravo.data.gui.ListCellRendererBasic;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.IRenderString;
import com.openbravo.data.loader.KeyBuilder;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorRecordAdapter;
import com.openbravo.pos.panels.ComboBoxCategoryFilter;
import com.openbravo.pos.panels.event.AbstractCategoryChangedListener;
import com.openbravo.pos.panels.event.CustomerChangedListener;

public class JCustomerNavigator extends JListNavigator implements AbstractCategoryChangedListener {
	I_DataLogic dataLogic;
	private ComboBoxCategoryFilter filter;
	private QBFParameters qbfParas=new QBFParameters();
	private QBFParameter nameLink=new QBFParameter(I_Customer.NAME);
	private QBFParameter name1Link=new QBFParameter(I_Customer.NAME1);
	private QBFParameter searchKeyLink=new QBFParameter(I_Customer.SEARCHKEY);
	
	public JCustomerNavigator(I_DataLogic dataLogic) {
		super(new BrowsableEditableDataModel(dataLogic,BusinessPartner.class,new EditorRecordAdapter<BusinessPartner>(),new DirtyManager()));
	    setCellRenderer(getListCellRenderer());
	    this.dataLogic=dataLogic;
	    qbfParas.or(nameLink).or(name1Link).or(searchKeyLink);
	}

	public void loadData() throws BasicException{
		m_bd.loadData(dataLogic.list(BusinessPartner.class));
		filter.loadData(dataLogic.list(CustomerCategory.class));
    }

	@Override
	public JPanel getNorthPanel() {
		JPanel filterPanel=new JPanel();
		filterPanel.setLayout(new javax.swing.BoxLayout(filterPanel, javax.swing.BoxLayout.Y_AXIS));
		final JTextField inputText=new JTextField();
		inputText.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					nameLink.link(inputText.getText());
					name1Link.link(inputText.getText());
					searchKeyLink.link(inputText.getText());
					//m_jlist.clearSelection();
					m_bd.loadData(dataLogic.query(BusinessPartner.class,qbfParas));
				} catch (BasicException ex) {
					ex.printStackTrace();
					new MessageInf(ex).show(JCustomerNavigator.this);
				}
			}
			
		});
		filterPanel.add(inputText);
		filter=new ComboBoxCategoryFilter();
		filter.addAbstractCategoryChangedListener(this);
		filterPanel.add(filter);
		return filterPanel;
	}

	@Override
	public void valueChanged(ListSelectionEvent evt) {
		super.valueChanged(evt);
		if (!evt.getValueIsAdjusting()){
			int i = m_jlist.getSelectedIndex();
            if (i >= 0) 
		      for(CustomerChangedListener l:listenerList.getListeners(CustomerChangedListener.class)){
			       l.valueSelectChanged((CustomerInfo) m_bd.getCurrentElement());
		     }
		}
	}
	
	public void addCustomerChangedListener(CustomerChangedListener l){
		listenerList.add(CustomerChangedListener.class, l);
	}
	
	public void removeCustomerChangedListener(CustomerChangedListener l){
		listenerList.remove(CustomerChangedListener.class, l);
	}
	
    public ListCellRenderer getListCellRenderer() {
        return new ListCellRendererBasic(new IRenderString<BusinessPartner>(){
			@Override
			public String getRenderString(BusinessPartner value) {	
				return value.getName();
			}
            
        });
    }

	@Override
	public void fireCategoryChanged(final I_Category selected) {
		try {
			//m_jlist.clearSelection();
			m_bd.loadData(dataLogic.listByParent(BusinessPartner.class,new I_ParentNode(){
				@Override
				public IKeyed getParentKey() {
					return KeyBuilder.getKey(selected.getID());
				}
				@Override
				public Class getParentClass() {
					return CustomerCategory.class;
				}
			}));
		} catch (BasicException e) {
			e.printStackTrace();
			new MessageInf(e).show(this);
		}
		
	}
}
